package com.qs.eggs.integral.fragment;

import android.content.Intent;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.qs.eggs.Constants;
import com.qs.eggs.R;
import com.qs.eggs.adapter.BaseAdapter;
import com.qs.eggs.base.fragment.BaseFragment;
import com.qs.eggs.integral.IntegralBean;
import com.qs.eggs.integral.activity.InegralDetialActivity;
import com.qs.eggs.integral.adapter.IntegralAdapter;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;

import okhttp3.Call;
import okhttp3.Response;

public class IntegralFragment extends BaseFragment{

    public static RecyclerView mRecyclerView;
    public static IntegralAdapter integralAdapter;
    public static IntegralFragment integralFragment;
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_integral;
    }

    @Override
    protected void initWidget(View root) {
        super.initWidget(root);
        integralFragment=this;
        mRecyclerView=findView(R.id.recycler);

    }

    @Override
    public void initData() {
        super.initData();
        loadData("");
    }

    public static void loadData(final String proname){
        HashMap<String,String> map=new HashMap<>();
        map.put("proname",proname);
        map.put("xiaoliang","");
        map.put("scoresort","desc");
        Gson gson=new Gson();
        String params=gson.toJson(map);
        OkGo.post(Constants.URL_BASE+"changezoneServlet?action=loadchangeProduct")
                .params("params",params)
                .params("rows","20")
                .params("page","1")
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        JSONObject jsonObject;
                        try {
                            jsonObject=new JSONObject(s);
                            JSONArray jsonArray=jsonObject.getJSONArray("rows");
                            Type type = new TypeToken<ArrayList<IntegralBean>>() {
                            }.getType();
                            GsonBuilder gsonBuilder = new GsonBuilder();
                            final ArrayList<IntegralBean> resultBean = gsonBuilder.create().fromJson(jsonArray.toString(), type);

                            integralAdapter=new IntegralAdapter(integralFragment.getContext(),resultBean);
//
                            mRecyclerView.setAdapter(integralAdapter);
                            mRecyclerView.setLayoutManager(new GridLayoutManager(integralFragment.getContext(), 2));
                            mRecyclerView.setItemAnimator(new DefaultItemAnimator());


                            integralAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
                                @Override
                                public void onItemClick(View view, int position) {
                                    Intent intent=new Intent(integralFragment.getContext(), InegralDetialActivity.class);
                                    intent.putExtra("proIntegral",resultBean.get(position).getDuihuanscore());
                                    intent.putExtra("proid",resultBean.get(position).getId());
                                    intent.putExtra("proname",resultBean.get(position).getProname());
                                    integralFragment.startActivity(intent);
                                }
                            });

                        } catch (JSONException e) {

                        }

                    }
                });
    }
}